import { createRouter, createWebHistory } from 'vue-router'

import Index from '../views/Index.vue'
import Login from '../views/Login.vue'
import register from '../views/Register.vue'
import weibo_detail from "../views/weibo_detail.vue"
import Weibo from '../views/WeiboAdd.vue'
import att from '../views/att_detai.vue'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/index',
      name: 'index',
      component: Index,
    },{
      path: '/login',
      name: 'login',
      component: Login,
    },{
      path: '/reg',
      name: 'register',
      component: register,
    },{
      path: '/weibo_add',
      name: 'weibo_add',
      component: Weibo,
      meta:{
        needLogin:true
      }
    },{
      path: '/weibo_detail/:id',
      name: 'weiboDetail',
      component: weibo_detail,
      meta:{
        needLogin:true
      }
    },{
      path: '/att',
      name: 'att',
      component: att,
      meta:{
        needLogin:true
      }
    },
  ],
})

router.beforeEach((to,from,next)=>{
  // alert("从"+from.name+"-->"+to.name);
  // if(to.name=='weibo_add' && !sessionStorage.getItem("mc"))
  // next('/login')
  // else
  // next();
  if(to.meta.needLogin && !sessionStorage.getItem("mc")){
    alert("请登录")
  next('/login')
  }else
  next();
})

export default router
